const { emailPattern } = require("/config");
const { request } = require("/utils");

Page({
    data: {
        field: 'username',
        value: '',
        initialValue: '',
    },
    onLoad(query) {
        const { field, label, value } = query;
        my.setNavigationBar({ title: `修改` + label });
        this.setData({ field, value, initialValue: value });
    },
    tapSave() {
        const { field, value, initialValue } = this.data;
        if (value === initialValue) return my.showToast({ content: '内容未修改' });
        if (!value) return my.showToast({ content: '内容不能为空' });
        if (field === 'email') {
            if (!emailPattern.test(value)) return my.showToast({ content: '邮箱格式错误' });
        }
        const pages = getCurrentPages();
        const pre = pages[pages.length - 2];
        pre.updateUser({
            [field]: value
        });
        my.navigateBack();
    },
    inputChange(e) {
        this.setData({
            value: e.detail.value
        })
    },
});